package net.iiyasvn.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.iiyasvn.R;
import net.iiyasvn.model.MessageInfo;
import net.iiyasvn.model.MessageInfoList;
import net.iiyasvn.utils.TextUtil;
import net.iiyasvn.utils.TimeUtil;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MessageAdapter extends BaseAdapter {
	private List<MessageInfo> info = null;
	private Map<Integer,View> rowViews = new HashMap<Integer,View>();
	private Context context = null;
	public MessageAdapter(MessageInfoList messageInfoList, Context context){
		info = messageInfoList.getInfo();
		this.context = context;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return info.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return info.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = rowViews.get(position);
		if(rowView == null){
			LayoutInflater layoutInflater = LayoutInflater.from(context);
			rowView = layoutInflater.inflate(R.layout.message_list_item, null);
			TextView nickname = (TextView)rowView.findViewById(R.id.nickname);
			TextView addtime = (TextView)rowView.findViewById(R.id.addtime);
			TextView content = (TextView)rowView.findViewById(R.id.content);
			MessageInfo messageInfo = (MessageInfo)getItem(position);
			if(messageInfo != null){
				nickname.setText(messageInfo.getNickname());
				content.setText(TextUtil.formatContent(messageInfo.getContent(),context));
				addtime.setText(TimeUtil.getTimeStr(messageInfo.getAddtime()));
	        }
				
			rowViews.put(position, rowView);
			}
		return rowView;
	}

}
